<template>
  <div id="list-contianer">
    <!--  -->
       <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!--  -->
      <el-card class="box-card">
            <el-row :gutter="20">
                  <el-col :span="10">
                      <!-- 搜索 -->
                    <el-input  v-model="queryInfo.query" clearable @clear="getGoodList" >
                        <el-button slot="append" icon="el-icon-search" @click="getGoodList" ></el-button>
                    </el-input>
                  </el-col>
                  <!-- 添加商品 -->
                  <el-col :span="4">
                   <el-button type="primary"  @click="goAddGoodsPage"> 添加商品</el-button>
                  </el-col>
           </el-row>
          <!-- 商品列表 -->

                    <el-table
              :data="goodList"
              border
              style="width: 100%">
              <el-table-column type="index" width="50">
              </el-table-column>
              <el-table-column prop="goods_name"  label="商品名称"  width="700">
              </el-table-column>
              <el-table-column prop="goods_price"  label="价格"  width="70">
              </el-table-column>
              <el-table-column prop="goods_weight"  label="重量"  width="70">
              </el-table-column>
              <el-table-column prop="add_time"  label="添加时间"  width="170">
                <template slot-scope="scope">
                  {{scope.row.add_time | dateFormat}}
                </template>
              </el-table-column>
              <el-table-column   label="操作"   >
                    <template slot-scope="scope">
                  <el-button type="primary" size="mini"  icon="el-icon-edit" @click="showEditGoodsDialog(scope.row)"></el-button>
                  <el-button type="danger"  size="mini" icon="el-icon-delete" @click="deleteUser(scope.row.goods_id)"></el-button>
                </template>
              </el-table-column>
             
            </el-table>

            <!-- 分页 -->
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.pagenum"
              :page-sizes="[5,10, 50, 100, 200,500]"
              :page-size="1000"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total" background>
            </el-pagination>
      </el-card>
      <!-- 修改商品弹窗 -->
       <el-dialog
        title="修改商品"
        :visible.sync="editGoodsDialogVisible"
        width="50%" @close = 'editGoodsDialogClose'>
        <!-- 内容主体区域 -->
            <el-form :model="editGoodsForm" :rules="editGoodsFormRules" ref="editGoodsFormRef" label-width="100px">
                        <!-- 用户名 -->
                <el-form-item label="商品ID"    >
                      <el-input  v-model="editGoodsForm.goods_id"    disabled></el-input>
                </el-form-item>
                <el-form-item label="商品名称"   prop="goods_name">
                      <el-input  v-model="editGoodsForm.goods_name"   clearable></el-input>
                </el-form-item>
                        <!-- 邮箱 -->
                <el-form-item  prop="goods_price" label="价格(￥)" >         
                     <el-input v-model="editGoodsForm.goods_price" clearable oninput="value=value.replace(/[^\d]/g,'')"   ></el-input>
                </el-form-item>
                        <!-- 电话 -->
                <el-form-item label="数量(件)" prop="goods_number">
                      <el-input  v-model.number="editGoodsForm.goods_number" oninput="value=value.replace(/[^\d]/g,'')"
                        clearable   ></el-input>
                </el-form-item>
                <el-form-item label="重量(KG)" prop="goods_weight">
                      <el-input  v-model.number="editGoodsForm.goods_weight" oninput="value=value.replace(/[^\d]/g,'')"
                        clearable   ></el-input>
                </el-form-item>
            </el-form>
            
        <!-- 内容底部区域 -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editGoodsDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editGoodsInfo">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
    data(){
      return{
        queryInfo:{
          query:'',
          pagenum:1,
          pagesize:10
        },
        goodList:[],
        total:0,
        editGoodsForm:{},
        editGoodsDialogVisible:false,
        // 添加商品
        addDialogVisible:false,
        addGoodsForm:{},

        editGoodsFormRules:{
            goods_name:[
             { required: true, message: '请输入商品名称', trigger: 'blur' },
            ],
            goods_price:[
               { required: true, message: '请输入价格', trigger: 'blur' },
            ],
            goods_weight:[
               { required: true, message: '请输入重量', trigger: 'blur' },
            ],
            goods_number:[
               { required: true, message: '请输入数量', trigger: 'blur' },
            ]
        }
      }
    },
    created(){
      this.getGoodList()
    },
    methods:{
        //获取数据
          async getGoodList(){
        const {data:res} = await this.$http.get(`goods`,{
          params: this.queryInfo
        })

        if(res.meta.status != 200 ) return this.$Message.error('商品列表数据获取失败')

        this.$Message.success('商品列表数据获取成功')
        this.goodList = res.data.goods;
        this.total = res.data.total;

        
      } ,
      handleSizeChange(newSize){
          this.queryInfo.pagesize =  newSize
          this.getGoodList()
      },
      handleCurrentChange(newPage){
        this.queryInfo.pagenum = newPage;
        this.getGoodList()
      },
       // 点击删除按钮
        // 触发弹窗询问
        deleteUser(id){
            this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then( async () => {
                 const  {data:res} = await this.$http.delete('goods/'+id)
                 if(res.meta.status !== 200) return this.$Message.error('删除商品失败！')
                 console.log(res);
                this.$Message({
                    type: 'success',
                    message: '删除商品成功!'
                });
                this.getGoodList()
                }).catch(() => {
                  this.$Message({
                      type: 'info',
                      message: '已取消删除操作'
                  });          
                });
            },
         
        showEditGoodsDialog(goodsinfo){
          this.editGoodsForm =goodsinfo;
          this.editGoodsDialogVisible = true
        },
        editGoodsDialogClose(){
          this.$refs.editGoodsFormRef.resetFields();
        },
        editGoodsInfo(){
            this.$refs.editGoodsFormRef.validate(async valid =>{
          this.editGoodsDialogVisible = false;
           if(!valid) return 
                const {data: res} = await this.$http.put(`goods/${this.editGoodsForm.goods_id}`,
                {
                  goods_name: this.editGoodsForm.goods_name,
                   goods_price: this.editGoodsForm.goods_price, 
                  goods_number: this.editGoodsForm.goods_number,
                   goods_weight: this.editGoodsForm.goods_weight
                })
                console.log(res);
                // if(res.meta.status !== 201) return this.$Message.error('更新商品信息失败！')
                 this.getGoodList()
                // this.$Message.success('更新商品信息成功！')
            })
      },
      goAddGoodsPage(){
        this.$router.push('goods/add')
      },

    }

}
</script>
<style lang="less">

.el-card{
    margin-top:20px;
    box-shadow: 0 0 3px #ddd;
}
.el-table,.el-pagination{
    margin-top: 20px;
}
.el-button{
  margin:0 15px;
}
</style>
